using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Ultimate.DAL.CollectionClasses;
using Ultimate.DAL;
using Ultimate.DAL.HelperClasses;
using Ultimate.DAL.EntityClasses;
using Ultimate.DAL.FactoryClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.Security;

using CommunityServer.Components;

namespace Ultimate.Controls
{
	/// <summary>
	///		Summary description for Menu.
	/// </summary>
	public class Menu : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.Literal ListItems;
		protected System.Web.UI.HtmlControls.HtmlAnchor A4;
		protected System.Web.UI.HtmlControls.HtmlAnchor A2;
		protected System.Web.UI.HtmlControls.HtmlAnchor A5;
		protected System.Web.UI.HtmlControls.HtmlAnchor A3;
		protected System.Web.UI.HtmlControls.HtmlAnchor A1;
        

		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!this.IsPostBack)
			{
				CreateMenu();
			}
		}

		private void CreateMenu()
		{
            string tmp = "";
            //tmp += "<li><a href=\"http://www.edmontonultimate.org\">Home</a></li>\n";
            if (Context.User.Identity.IsAuthenticated)
            {

                //tmp += "<li><a href=\"" + this.ResolveUrl("~/RegistrationSystem/Player/EditPlayer.aspx") + "\">My Details</a></li>\n";
                //tmp += "<li><a href=\"" + this.ResolveUrl("~/RegistrationSystem/League/JoinLeague.aspx") + "\">Join League</a></li>\n";

                //Find all the leagues they belong too

                //List the team they belong to on that league
                MembershipUser mu = Membership.GetUser();
                Guid userID = (Guid)mu.ProviderUserKey;
                

               
                IRelationCollection relations = new RelationCollection();
                relations.Add(TeamEntity.Relations.TeamPlayerEntityUsingTeamID);
                relations.Add(TeamPlayerEntity.Relations.PlayerEntityUsingUserID);

                TeamCollection tc = new TeamCollection();
                PredicateExpression A = new PredicateExpression();
                A.Add(PredicateFactory.CompareValue(PlayerFieldIndex.UserID  , ComparisonOperator.Equal, userID));

                ISortExpression sorter = new SortExpression(SortClauseFactory.Create(TeamFieldIndex.TeamName, SortOperator.Ascending));

                tc.GetMulti(A, 10000, sorter, relations);
                foreach (TeamEntity t in tc.Items)
                {
                    tmp += "<li><a href=\"" + this.ResolveUrl("~/RegistrationSystem/League/EditLeague.aspx?leagueID=" + t.LeagueID.ToString()) + "\">" + t.League.Name + "</a></li>\n";
                    tmp += "<ul><li><a href=\"" + this.ResolveUrl("~/RegistrationSystem/Team/Editteam.aspx?teamID=" + t.TeamID.ToString()) + "\">" + t.TeamName + "</a></li></ul>\n";
                }
            }
            if (CSContext.Current.User.IsRegistrationAdministrator)
            {
                tmp += "<li><a href=#>Admin</a></li>\n";
                tmp += "<ul>\n";
                tmp += "<li><a href=\"" + this.ResolveUrl("~/RegistrationSystem/Player/AdminPlayers.aspx") + "\">" + "Players" + "</a></li>\n";
                LeagueCollection lc = new LeagueCollection();

                ISortExpression sorter = new SortExpression(SortClauseFactory.Create(LeagueFieldIndex.Name, SortOperator.Ascending));

                lc.GetMulti(null, 10000, sorter);
                if (lc.Items.Count > 0)
                {
                    foreach (LeagueEntity le in lc.Items)
                    {
                        tmp += "<li><a href=\"" + this.ResolveUrl("~/RegistrationSystem/League/EditLeague.aspx?leagueID=" + le.LeagueID.ToString()) + "\">" + le.Name + "</a></li>\n";
                    }
                }
                tmp += "</ul>";
            }
            //if (Context.User.Identity.IsAuthenticated)
            //{
            //    tmp += "<li><a href=\"" + this.ResolveUrl("~/RegistrationSystem/Login/Logout.aspx") + "\">Log Off</a></li>\n";
            //}
            ListItems.Text = tmp;
		}
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}
